import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

 console.log(to)
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      NProgress.done()
      next()
    } else {
      const hasGetUserInfo = localStorage.getItem('user')
      console.log(hasGetUserInfo)
      if (hasGetUserInfo) {
        next()
      } else {
        next(`/login?redirect=${to.path}`)
          NProgress.done()

      }
    }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
